package com.rds.eco.liftcycle;//package com.rds.eco.liftcycle;
//
//import com.rds.common.api.eco.domain.RdaEco;
//import com.rds.common.api.eco.domain.RdaEcoRel;
//import com.rds.common.api.workspace.domain.MaterialBill;
//import com.rds.common.core.domain.AjaxResult;
//import com.rds.common.core.domain.entity.Document;
//import com.rds.common.utils.DateUtils;
//import com.rds.common.utils.rds.LifecycleUtil;
//import com.rds.common.utils.uuid.SnowFlakeUtils;
//
//import com.rds.eco.service.IRdaEcoRelService;
//import com.rds.eco.service.IRdaEcoService;
//import com.rds.material.liftcycle.partLifeCycleUtil;
//import com.rds.material.service.IMaterialBillService;
//import com.rds.material.service.IRdaPartsRelService;
////import com.rds.system.common.LifecycleUtil;
//import com.rds.system.common.history.HistoryRecordUtil;
//import com.rds.system.service.*;
//import com.rds.workspace.lifecycle.WorkspaceUtils;
//import org.springframework.beans.BeanUtils;
//
//import java.util.List;
//import java.util.Map;
//
///**
// * 此类主要用作生命周期相关操作(单独给物料用)
// * @version : 1.0
// * @Copyright: Copyright(c) 2020-2021 All right reserved
// * @Company: GUANGZHOU RDS Co.,LTD
// * @author: Lijiasen
// * @create Date : 2022/12/11 : 22:50
// */
//
///**
// *zheng'qu
// */
//public class ecoCycleUtil {
//    private final IRdaStateService rdaStateService;
//
//    /**历史记录持久化 2021-12-13**/
//    private ISysHistoryService sysHistoryService;
//
//    private IRdaEcoRelService rdaEcoRelService;
//
//
//    private IRdaEcoService rdaEcoService;
//
//    private IRdaTypeService rdaTypeService;
//
//    private IRdaPolicyService rdaPolicyService;
//
//    private IRdaPartsRelService rdaPartsRelService;
//
//    private IMaterialBillService materialBillService;
//
//    private IDocumentService documentService;
//
//
//    public ecoCycleUtil(IRdaStateService  rdaStateService){
//        this.rdaStateService = rdaStateService;
//    }
//
//    /*************************************** 历史记录写进数据库-持久化 2021-12-13 [begin] **********************************************************/
//    public ecoCycleUtil(IRdaStateService rdaStateService, ISysHistoryService sysHistoryService, IRdaEcoRelService rdaEcoRelService, IRdaEcoService rdaEcoService, IRdaTypeService rdaTypeService, IRdaPolicyService rdaPolicyService){
//        this.rdaStateService = rdaStateService;
//        this.sysHistoryService = sysHistoryService;
//        this.rdaEcoRelService = rdaEcoRelService;
//        this.rdaEcoService = rdaEcoService;
//        this.rdaTypeService = rdaTypeService;
//        this.rdaPolicyService = rdaPolicyService;
//    }
//
//    public ecoCycleUtil(IRdaStateService rdaStateService, ISysHistoryService sysHistoryService, IRdaEcoRelService rdaEcoRelService, IRdaEcoService rdaEcoService, IRdaTypeService rdaTypeService, IRdaPolicyService rdaPolicyService, IRdaPartsRelService rdaPartsRelService, IMaterialBillService materialBillService, IDocumentService documentService){
//        this.rdaStateService = rdaStateService;
//        this.sysHistoryService = sysHistoryService;
//        this.rdaEcoRelService = rdaEcoRelService;
//        this.rdaEcoService = rdaEcoService;
//        this.rdaTypeService = rdaTypeService;
//        this.rdaPolicyService = rdaPolicyService;
//        this.rdaPartsRelService = rdaPartsRelService;
//        this.materialBillService = materialBillService;
//        this.documentService = documentService;
//    }
//
//
//
//    /*************************************** 历史记录写进数据库-持久化 2021-12-13 [end] **********************************************************/
//    /**
//     * 当前方法只针对流程对象提升/降低生命周期操作
//     * @param id 当前对象Id
//     * @param policyid 策略Id
//     * @param event 提升/降级
//     * @param moduleName 模块名称
//     * @param appointTargetStateId 指定到的状态Id
//     * @return 提升信息
//     */
//    public AjaxResult goToTargetState(Long id, Long policyid, String event, String moduleName,Long appointTargetStateId){
//        if("eco".equalsIgnoreCase(moduleName)){
//            RdaEco rdaEco = rdaEcoService.selectRdaEcoById(id);
//            int currentStateId = rdaEco.getStateid();
//            System.out.println("PartLifeCycleUtil[74]:"+event+"--->>>currentStateId:" + currentStateId);
//            /*获取当前策略下的所有状态**/
//            Map<String, Object> eventMap = getToBeSetStateMap(Long.parseLong(rdaEco.getPolicyid().toString()),currentStateId,event,appointTargetStateId);
//            if ("0".equals(String.valueOf(eventMap.get("code")))) {
//                int targetStateId = parseString2Int(String.valueOf(eventMap.get("targetStateId")));
//                rdaEco.setStateid(targetStateId);
//                int rows = rdaEcoService.updateRdaEco(rdaEco);
//                new HistoryRecordUtil(rdaStateService,sysHistoryService).lifeCycleHistory(id,currentStateId,targetStateId,event);
//                return  rows > 0 ? AjaxResult.success((String) eventMap.get("msg")) : AjaxResult.error("提升失败!");
//            } else if ("-1".equals(String.valueOf(eventMap.get("code")))) {
//                return AjaxResult.error((String) eventMap.get("msg"));
//            }else{
//                return AjaxResult.error( "出现未知错误,请联系管理员!");
//            }
//        } else {
//            return AjaxResult.success(event+"对象类型不存在,请联系管理员!");
//        }
//    }
//
//    /**
//     * 处理当前对象是否是提升/降级
//     * @param policyid 策略Id
//     * @param currentStateId 当前状态Id
//     * @param event 提升/降级
//     * @param appointTargetStateId 指定到的状态Id
//     * @return 提升/降级 后的状态Id 集合
//     */
//    private Map<String, Object> getToBeSetStateMap(Long policyid,int currentStateId,String event,Long appointTargetStateId){
//        /*这里的sort 必须在创建状态的时候按照顺序来,即使是后面新增的状态,状态sort值也得修改 同一策略的状态sort 不能一样**/
//        Long selectStateId = parseInt2Long(currentStateId);
//        int currentSort = rdaStateService.selectRdaStateById(selectStateId).getSort();
//        //System.out.println("LifeCycleController:promote--->>>currentSort:" + currentSort);
//        /*获取当前策略下的所有状态**/
//        LifecycleUtil lifecycleUtil = new LifecycleUtil(rdaStateService);
//        return lifecycleUtil.goToAppointTargetState(policyid, event, currentSort,appointTargetStateId);
//    }
//
//    public void affectedProjects(Long id, Long policyid, String event, String moduleName,Long appointTargetStateId){
//        RdaEco rdaEco = rdaEcoService.selectRdaEcoById(id);
//        int currentStateId = rdaEco.getStateid();
//        System.out.println("PartLifeCycleUtil[74]:"+event+"--->>>currentStateId:" + currentStateId);
//        //查找eco里面说有受影响的项目
//        RdaEcoRel rdaEcoRel = new RdaEcoRel();
//        rdaEcoRel.setToid(id);
//        List<RdaEcoRel> rdaEcoRels = rdaEcoRelService.selectRdaEcoRelList(rdaEcoRel);
//        //目前eco里面引用：1物料，2文档，根据typeid去区分
//        Map<String,Object> part= WorkspaceUtils.getTypePolicyStates("EC Part","1",rdaTypeService,rdaPolicyService,rdaStateService);
//        Map<String,Object> document= WorkspaceUtils.getTypePolicyStates("Document","1",rdaTypeService,rdaPolicyService,rdaStateService);
//        Map<String,Object> eco= WorkspaceUtils.getTypePolicyStates("typeByEco","1",rdaTypeService,rdaPolicyService,rdaStateService);
//        String partType=((Map<String,Long>)part.get("Typeid")).get("Part").toString();
//        String documentType=((Map<String,Long>)document.get("Typeid")).get("Document").toString();
//        //发布
//        String partStateid = ((Map<String, Long>) part.get("Stateid")).get("Release").toString();
//        //初步
//        String partStateidF = ((Map<String, Long>) part.get("Stateid")).get("Preliminary").toString();
//        //废弃
//        String partStateidO = ((Map<String, Long>) part.get("Stateid")).get("Obsolete").toString();
//        //发布
//        String documentStateid = ((Map<String, Long>) document.get("Stateid")).get("Release").toString();
//        //创建
//        String documentStateidF = ((Map<String, Long>) document.get("Stateid")).get("Create").toString();
//        //废弃
//        String documentStateidO = ((Map<String, Long>) document.get("Stateid")).get("Obsoleted").toString();
//        String ecoStateid = ((Map<String, Long>) eco.get("Stateid")).get("Published").toString();
//        for (RdaEcoRel ecoRel : rdaEcoRels) {
//            System.out.println(ecoRel);
//            MaterialBill materialBill = materialBillService.selectMaterialBillById(ecoRel.getFromid());
//            Document documents = documentService.selectDocumentById(ecoRel.getFromid());
//            if (ecoStateid.equals(currentStateId)){//如果eco发布状态
//                //那里面说有受影响项都应该是发布状态
//                if (ecoRel.getFromtype().toString().equals(partType)){//物料
//                    materialBill.setStateid(Integer.parseInt(partStateid));
//                    materialBill.setCreateTime(DateUtils.getNowDate());
//                    materialBillService.updateMaterialBill(materialBill);
//                }else if(ecoRel.getFromtype().toString().equals(documentType)){//文档
//                    documents.setStateid(Integer.parseInt(documentStateid));
//                    documents.setCreateTime(DateUtils.getNowDate());
//                    documentService.updateDocument(documents);
//                }
//            }else{//如果是其他状态，需要根据请求变更的内容来处理逻辑
//                //1.如果是更改，则需要升级版本，然后对新版本进行升级
//                if (ecoRel.getRequestChange().equals("更改")){
//                    if (ecoRel.getFromtype().toString().equals(partType)){
//                        //第一次升级修订版为null
//                        if (ecoRel.getRevision()== null){
//                            String currentRevision  = materialBill.getRevision();
//                            char newRevision = currentRevision.charAt(0);
//                            if (Character.isDigit(newRevision)) {
//                                newRevision++;
//                            } else if (Character.isLetter(newRevision)) {
//                                newRevision = (char) (newRevision + 1);
//                            }
//                            ecoRel.setRevision(String.valueOf(newRevision));
//                            rdaEcoRelService.updateRdaEcoRel(ecoRel);
//                            MaterialBill materialBill1 = new MaterialBill();
//                            BeanUtils.copyProperties(materialBill,materialBill1);
//                            long snowId = SnowFlakeUtils.nextId();
//                            materialBill1.setId(snowId);
//                            materialBill1.setStateid(Integer.parseInt(partStateidO));
//                            materialBill1.setUpdateTime(DateUtils.getNowDate());
//                            materialBill.setStateid(Integer.parseInt(partStateidF));
//                            materialBill.setRevision(String.valueOf(newRevision));
//                            materialBillService.updateMaterialBill(materialBill);
//                            materialBillService.insertMaterialBills(materialBill1);
//                        }
//                        //处于发布状态才能升级
//                        if ((event.equals("demote")&&!materialBill.getStateid().toString().equals(partStateidF))||(event.equals("promote")&&!materialBill.getStateid().toString().equals(partStateid))){
//                            partLifeCycleUtil partLifeCycle = new partLifeCycleUtil(rdaStateService,sysHistoryService,rdaPartsRelService,materialBillService,rdaTypeService,rdaPolicyService);
//                            partLifeCycle.goToTargetStates(ecoRel.getFromid(), policyid, event, "part", 0L);
//                        }
//                    }else if(ecoRel.getFromtype().toString().equals(documentType)){
//                        if (ecoRel.getVersion()== null){
//                            Document document1 = new Document();
//                            BeanUtils.copyProperties(documents,document1);
//                            long snowId = SnowFlakeUtils.nextId();
//                            document1.setId(snowId);
//                            document1.setStateid(Integer.parseInt(documentStateidO));
//                            document1.setUpdateTime(DateUtils.getNowDate());
//                            String currentRevision  = documents.getRevision();
//                            char newRevision = currentRevision.charAt(0);
//                            if (Character.isDigit(newRevision)) {
//                                newRevision++;
//                            } else if (Character.isLetter(newRevision)) {
//                                newRevision = (char) (newRevision + 1);
//                            }
//                            documents.setStateid(Integer.parseInt(documentStateidF));
//                            documents.setRevision(String.valueOf(newRevision));
//                        }
//                        if ((event.equals("demote")&&!documents.getStateid().toString().equals(documentStateidF))||(event.equals("promote")&&!documents.getStateid().toString().equals(documentStateidO))){
//                            LifecycleUtil docLifecycle = new LifecycleUtil(rdaStateService,documentService,sysHistoryService);
//                            docLifecycle.goToTargetState(ecoRel.getFromid(),policyid,event,"document",false);
//                        }
//                    }
//                }else {//2.如果是发布，则状态升级就行
//                    if (ecoRel.getFromtype().toString().equals(partType)){
//                        if ((event.equals("demote")&&!materialBill.getStateid().toString().equals(partStateidF))||(event.equals("promote")&&!materialBill.getStateid().toString().equals(partStateid))){
//                            partLifeCycleUtil partLifeCycle = new partLifeCycleUtil(rdaStateService,sysHistoryService,rdaPartsRelService,materialBillService,rdaTypeService,rdaPolicyService);
//                            partLifeCycle.goToTargetStates(ecoRel.getFromid(), policyid, event, "part", 0L);
//                        }
//                    }else if(ecoRel.getFromtype().toString().equals(documentType)){
//                        if ((event.equals("demote")&&!documents.getStateid().toString().equals(documentStateidF))||(event.equals("promote")&&!documents.getStateid().toString().equals(documentStateidO))){
//                            LifecycleUtil docLifecycle = new LifecycleUtil(rdaStateService,documentService,sysHistoryService);
//                            docLifecycle.goToTargetState(ecoRel.getFromid(),policyid,event,"document",false);
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    private Long parseInt2Long(int stateId){
//        return Long.parseLong(String.valueOf(stateId));
//    }
//
//    private Integer parseString2Int(String stateId){
//        return Integer.parseInt(stateId);
//    }
//
//    private Integer parseLong2Int(Long stateId){return Integer.parseInt(String.valueOf(stateId));
//    }
//
//
//}
